package net.huashitong.synthetical.newSupervision.controller;

import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.JqGrid;
import com.sirdc.modules.sys.entity.SysDept;
import com.sirdc.modules.sys.entity.SysLogin;
import com.sirdc.modules.sys.service.SysDeptService;
import com.sirdc.modules.sys.service.SysLoginService;
import com.sirdc.modules.sys.util.SysUserUtils;
import net.huashitong.synthetical.newSupervision.entity.TaskNew;
import net.huashitong.synthetical.newSupervision.entity.TaskSend;
import net.huashitong.synthetical.newSupervision.entity.TaskWorkable;
import net.huashitong.synthetical.newSupervision.filter.TaskNewFilter;
import net.huashitong.synthetical.newSupervision.service.TaskNewService;
import net.huashitong.synthetical.newSupervision.service.TaskSendService;
import net.huashitong.synthetical.newSupervision.service.TaskWorkableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: ling
 * Date: 2019/7/23
 * Time: 16:24
 * To change this template use File | Settings | File Templates.
 * Description:逾期任务控制层
 */
@Controller
@RequestMapping("/taskOverdue")
public class TaskOverdueController extends JsonBaseController{

    @Autowired
    private TaskNewService taskNewService;
    @Autowired
    private TaskSendService taskSendService;
    @Autowired
    private SysDeptService sysDeptService;
    @Autowired
    private SysLoginService sysLoginService;

    @Autowired
    private TaskWorkableService taskWorkableService;

    @Override
    protected String getView(String s) {
        return "/modules/project/taskOverdue/"+s;
    }
    @RequestMapping(method = RequestMethod.GET)
    public String initPage() {
        return getView("query");
    }

    @RequestMapping("/filter")
    public String filter() {
        return getView("filter");
    }

    /**
     * 督查组的逾期任务列表
     * @param taskNewFilter
     * @return
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST)
    public JqGrid taskOverdue(TaskNewFilter taskNewFilter){
        String userId = SysUserUtils.getUserId();
        SysLogin login = sysLoginService.getById(userId);
        String deptId = login.getDeptId();
        SysDept dept = sysDeptService.getById(deptId);
        List<TaskNew> projectList = new ArrayList<>();

        //判断该用户是督查组还是单位用户
        if ("/01/".equals(dept.getDeptRelation())){
            //如果上级单位id为01,表示该用户是督查组人员
            //设置条件为逾期
            taskNewFilter.setStatus("0");
            if ("01".equals(login.getRoleId())) {
                taskNewFilter.setInspectionId(deptId);
                projectList = taskNewService.getProjectList(taskNewFilter);
            } else {
                taskNewFilter.setOperatorId(userId);
                projectList = taskNewService.getProjectList(taskNewFilter);
            }
        }else{
            //如果不是,那么为单位用户
            taskNewFilter.setUserId(userId);
            taskNewFilter.setStatus("0");
            projectList = taskSendService.queryFilter(taskNewFilter);
        }

        if ("admin".equals(login.getUsername())) {
            taskNewFilter.setStatus("0");
            projectList = taskNewService.getProjectList(taskNewFilter);
        }
        return coverJqGrid(taskNewFilter, projectList);
    }
    /**
     * 督查组查看逾期任务详情
     * @param id  主任务主键id
     * @return
     */
    @RequestMapping(value = "/detail/{id}",method = RequestMethod.GET)
    public String overdueDetail(Model model,@PathVariable String id){
        TaskNew task = taskNewService.getById(id);
        List<TaskSend> sendList = taskSendService.queryByTaskId(id);
        List<TaskWorkable> workableList = taskWorkableService.queryByTaskId(id);
        model.addAttribute("taskNew", task);
        model.addAttribute("sendList", sendList);
        model.addAttribute("workableList", workableList);
        return getView("detail");
    }
}
